require(['./config'], () => {
    require(['template', 'bootstrap', 'distdata', 'dist', 'header', 'footer'], (template) => {
        class Cart {
            constructor() {
                this.init()
                this.calcCartNum()
                this.address()
                this.addAddress()
                this.delAddress()
            }
            init() {
                let cartList = localStorage.getItem('cart')
                if (cartList) {
                    this.cartList = JSON.parse(cartList)
                    $('#cartPay').html(template('cartShop', { cartList: this.cartList }))
                        // 有数据的话就要默认计算一次总价
                    this.calcTotalMoney()
                } else {
                    // 购物车没有数据
                    $('#cart-empty').show()
                }
            }
            calcTotalMoney() {
                $('.money').html(this.cartList.reduce((money, cart) => {
                    money += cart.price * cart.num
                    return money
                }, 0).toFixed(2))
            }
            calcCartNum() {
                    let cartList = localStorage.getItem('cart')
                    cartList = JSON.parse(cartList)
                    let num = cartList.reduce((total, cart) => {
                        total += cart.num
                        return total;
                    }, 0)
                    $('#shopnum').html(num)

                }
                // 模态框
            mapShow() {
                $('#target').distpicker({
                    province: '---- 所在省 ----',
                    city: '---- 所在市 ----',
                    district: '---- 所在区 ----'
                });
            }
            address() {
                $('.save').on('click', function() {
                    let addrlist = localStorage.getItem('address')
                    if (addrlist) {
                        addrlist = JSON.parse(addrlist)
                        const isExist = addrlist.some(address => {
                            return address.username === $('#name').val()
                        })
                        if (isExist) {
                            addrlist = addrlist.map(address => {
                                if (address.username === $('#name').val()) {

                                }
                                return address
                            })
                        } else {
                            // 没有当前地址
                            addrlist.push({
                                username: $('#name').val(),
                                phone: $('#phone').val(),
                                pro: $('#pro').val(),
                                city: $('#city').val(),
                                town: $('#town').val(),
                                addr: $('#addr').val(),
                                townCode: $('#townCode').val(),
                            })
                        }
                        localStorage.setItem('address', JSON.stringify(addrlist))
                    } else {
                        localStorage.setItem('address', JSON.stringify([{
                            username: $('#name').val(),
                            phone: $('#phone').val(),
                            pro: $('#pro').val(),
                            city: $('#city').val(),
                            town: $('#town').val(),
                            addr: $('#addr').val(),
                            townCode: $('#townCode').val(),
                        }]))
                    }
                })
            }
            addAddress() {
                let addrlist = localStorage.getItem('address')
                this.addrlist = JSON.parse(addrlist)
                $('#addr-list').html(template('addrInfo', { addrlist: this.addrlist }))

            }
            delAddress() {
                $('.addr-del').on('click', function() {
                    let thisUsername = $($(this).parents('li').children()[0]).html()
                    let addlist = localStorage.getItem('address')
                    if (addlist) {
                        addlist = JSON.parse(addlist)
                        if (confirm('确定要删除吗?')) {
                            addlist = addlist.filter(addr => {
                                return addr.username != thisUsername
                            })
                            $(this).parents('li').remove()
                        }
                        console.log(addlist)
                        localStorage.setItem('address', JSON.stringify(addlist))
                    }
                })

            }
        }
        new Cart()
    })
})